﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYFamilyLibDataManager;

namespace YArchitech.Plumbing
{
	public partial class FormBathroomMore : System.Windows.Forms.Form
	{
		public FormBathroomMore()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
		}

		public FormBathroomMore(TreeView tv, Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = doc;
			base.Icon = Resource.YArchitechIcon;
			this.hvacDR = new HVACDataReader(this.doc);
			this.treeView = tv;
			base.Controls.Add(this.treeView);
			this.initTreeView();
			this.initFamilyTypeCB();
			this.InitFromData();
		}

		private void initTreeView()
		{
			this.treeView.Location = new System.Drawing.Point(4, 16);
			this.treeView.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.treeView.Width = 168;
			this.treeView.Height = 237;
			this.treeView.NodeMouseClick += this.treeView_NodeMouseClick;
		}

		private void initFamilyTypeCB()
		{
			TreeNode treeNode = null;
			if (this.treeView.Nodes.Count > 0)
			{
				treeNode = TreeViewTools.GetFirstNode(this.treeView.Nodes[0]);
				this.treeView.SelectedNode = treeNode;
			}
			this.DisPlayImgAndParameter(treeNode);
		}

		private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			this.DisPlayImgAndParameter(e.Node);
		}

		protected void DisPlayImgAndParameter(TreeNode treeNode)
		{
			try
			{
				if (treeNode != null && treeNode.Tag is DbFamily)
				{
					Image image = (treeNode.Tag as DbFamily).Image;
					this.picBoxpreview.Image = image;
					this.DisPlayFamilyType(treeNode);
					if (this.treeSelectedNode != null)
					{
						this.treeSelectedNode.BackColor = System.Drawing.Color.Transparent;
						this.treeSelectedNode.ForeColor = System.Drawing.Color.Black;
					}
					this.treeSelectedNode = treeNode;
					treeNode.BackColor = System.Drawing.Color.DodgerBlue;
					treeNode.ForeColor = System.Drawing.Color.White;
					if (this.listBoxFamilySybol.SelectedItem == null && this.listBoxFamilySybol.Items.Count > 0)
					{
						this.listBoxFamilySybol.SelectedItem = this.listBoxFamilySybol.Items[0];
					}
				}
			}
			catch (Exception)
			{
			}
		}

		private void DisPlayFamilyType(TreeNode treeNode)
		{
			if (treeNode != null && treeNode.Tag is DbFamily)
			{
				this.listBoxFamilySybol.Items.Clear();
				List<string> symbols = (treeNode.Tag as DbFamily).Symbols;
				IComparer<string> comparer = new ListSortByNumber();
				symbols.Sort(comparer);
				foreach (string item in symbols)
				{
					this.listBoxFamilySybol.Items.Add(item);
				}
			}
		}

		private void FamilyLoad()
		{
			DbFamily family = this.treeSelectedNode.Tag as DbFamily;
			string typeName = this.listBoxFamilySybol.SelectedItem.ToString();
			DbFamilySymbol familySymbol = this.hvacDR.GetFamilySymbol(family, typeName);
			if (familySymbol != null)
			{
				this.fs = familySymbol.Create(this.doc);
				if (this.fs == null)
				{
					throw new Exception("加载族失败");
				}
			}
			else
			{
				MessageBox.Show("读取族库失败", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			}
		}

		private void AlongTypeChanged(object sender, EventArgs e)
		{
			int tabIndex = (sender as RadioButton).TabIndex;
			if (this.rad_LayoutNum.Checked)
			{
				this.label_Layout.Text = "个数：";
				this.textBox_LayoutNum.Visible = true;
				this.textBox_LayoutDis.Visible = false;
				this.label_LayoutTwo.Text = "首个卫浴与起点的距离\n和卫浴间距比值：";
				this.textBox_Layout.Visible = false;
				this.textBox_LayoutTwo.Visible = true;
				return;
			}
			this.label_Layout.Text = "间距(mm)：";
			this.textBox_LayoutNum.Visible = false;
			this.textBox_LayoutDis.Visible = true;
			this.label_LayoutTwo.Text = "首个卫浴与起点的距离\n(mm)：";
			this.textBox_Layout.Visible = true;
			this.textBox_LayoutTwo.Visible = false;
		}

		private void buttonAngel_Click(object sender, EventArgs e)
		{
			ViewPlan viewPlan = this.doc.ActiveView as ViewPlan;
			if (viewPlan == null || (int)viewPlan.ViewType != 1)
			{
				YJKMessageBox.Show("手动测量只能在平面视图下可用！");
				return;
			}
			base.DialogResult = DialogResult.Abort;
		}

		private bool CheckData(int nDrawType)
		{
			if (this.listBoxFamilySybol.SelectedItems == null || this.listBoxFamilySybol.SelectedItems.Count <= 0)
			{
				YJKMessageBox.Show("请选择要布置的卫浴类型！");
				return false;
			}
			if (string.IsNullOrEmpty(this.textBoxHeigh.Text))
			{
				YJKMessageBox.Show("相对标高不能为空！");
				return false;
			}
			this.plumbingFixturesPara.OffSet = Convert.ToDouble(this.textBoxHeigh.Text);
			if (this.checkAngel.Checked)
			{
				if (string.IsNullOrEmpty(this.textBoxAngel.Text) || Convert.ToInt32(this.textBoxAngel.Text) < 0 || Convert.ToInt32(this.textBoxAngel.Text) > 360)
				{
					YJKMessageBox.Show("旋转角度应在0°~360°之间！");
					return false;
				}
				this.plumbingFixturesPara.Angle = Convert.ToDouble(this.textBoxAngel.Text);
			}
			else
			{
				this.plumbingFixturesPara.Angle = 0.0;
			}
			if (nDrawType == 0)
			{
				return true;
			}
			if (this.rad_LayoutNum.Checked)
			{
				this.LineParameter.LimitFlag = 1;
				if (string.IsNullOrEmpty(this.textBox_LayoutTwo.Text) || Convert.ToInt32(this.textBox_LayoutTwo.Text) < 1)
				{
					YJKMessageBox.Show("卫浴个数应大于0！");
					return false;
				}
				if (string.IsNullOrEmpty(this.textBox_LayoutNum.Text))
				{
					YJKMessageBox.Show("间距比值不能为空！");
					return false;
				}
				this.LineParameter.Count = Convert.ToInt32(this.textBox_LayoutTwo.Text);
				this.LineParameter.Ratio = Convert.ToDouble(this.textBox_LayoutNum.Text);
			}
			else
			{
				this.LineParameter.LimitFlag = 2;
				if (string.IsNullOrEmpty(this.textBox_Layout.Text) || Convert.ToInt32(this.textBox_Layout.Text) < 100)
				{
					YJKMessageBox.Show("卫浴间距不应小于100mm！");
					return false;
				}
				if (string.IsNullOrEmpty(this.textBox_LayoutDis.Text))
				{
					YJKMessageBox.Show("首个卫浴与起点距离不能为空！");
					return false;
				}
				this.LineParameter.Distance = (double)Convert.ToInt32(this.textBox_Layout.Text);
				this.LineParameter.DistanceSide = (double)Convert.ToInt32(this.textBox_LayoutDis.Text);
			}
			return true;
		}

		private void btnSingle_Click(object sender, EventArgs e)
		{
			this.layTerminalType = 0;
			if (!this.CheckData(this.layTerminalType))
			{
				return;
			}
			this.FamilyLoad();
			base.DialogResult = DialogResult.OK;
		}

		private void btnAlong_Click(object sender, EventArgs e)
		{
			this.layTerminalType = 1;
			if (!this.CheckData(this.layTerminalType))
			{
				return;
			}
			this.FamilyLoad();
			base.DialogResult = DialogResult.OK;
		}

		private void InitFromData()
		{
			UserSetting userSetting = new UserSetting();
			string text = userSetting.GetValue("FormBathroomLayout", "textBoxHeigh");
			if (string.IsNullOrEmpty(text))
			{
				text = "0.0";
			}
			this.textBoxHeigh.Text = text;
			text = userSetting.GetValue("FormBathroomLayout", "checkAngel");
			this.checkAngel.Checked = text.Equals(true.ToString());
			this.checkAngel_CheckedChanged(null, null);
			text = userSetting.GetValue("FormBathroomLayout", "textBoxAngel");
			if (string.IsNullOrEmpty(text))
			{
				text = "0";
			}
			this.textBoxAngel.Text = text;
			this.rad_LayoutDist.Checked = (1 == Convert.ToInt32(userSetting.GetValue("FormBathroomLayout", "alongType")));
			text = userSetting.GetValue("FormBathroomLayout", "textBox_LayoutNum");
			if (string.IsNullOrEmpty(text))
			{
				text = "0.5";
			}
			this.textBox_LayoutNum.Text = text;
			text = userSetting.GetValue("FormBathroomLayout", "textBox_LayoutDis");
			if (string.IsNullOrEmpty(text))
			{
				text = "400";
			}
			this.textBox_LayoutDis.Text = text;
			text = userSetting.GetValue("FormBathroomLayout", "label_Layout_Num");
			if (string.IsNullOrEmpty(text))
			{
				text = "2";
			}
			this.textBox_LayoutTwo.Text = text;
			text = userSetting.GetValue("FormBathroomLayout", "label_Layout_Dis");
			if (string.IsNullOrEmpty(text))
			{
				text = "800";
			}
			this.textBox_Layout.Text = text;
			if (this.rad_LayoutNum.Checked)
			{
				this.textBox_LayoutNum.Visible = true;
				this.textBox_LayoutDis.Visible = false;
				this.textBox_Layout.Visible = false;
				this.textBox_LayoutTwo.Visible = true;
				return;
			}
			this.textBox_LayoutNum.Visible = false;
			this.textBox_LayoutDis.Visible = true;
			this.textBox_Layout.Visible = true;
			this.textBox_LayoutTwo.Visible = false;
		}

		private void checkAngel_CheckedChanged(object sender, EventArgs e)
		{
			if (this.checkAngel.Checked)
			{
				this.textBoxAngel.Enabled = true;
				this.buttonAngel.Enabled = true;
				return;
			}
			this.textBoxAngel.Enabled = false;
			this.buttonAngel.Enabled = false;
		}

		public void SetAngelFromPoint(XYZ startPnt, XYZ endPnt)
		{
			double num = 0.0;
			XYZ xyz = new XYZ(startPnt.X, startPnt.Y, 0.0);
			XYZ xyz2 = new XYZ(endPnt.X, endPnt.Y, 0.0).Subtract(xyz).Normalize();
			if (Math.Abs(xyz2.X) < 0.0001)
			{
				if (xyz2.Y > 0.0)
				{
					num = 90.0;
				}
				if (xyz2.Y < 0.0)
				{
					num = 270.0;
				}
				if (xyz2.Y == 0.0)
				{
					num = 0.0;
				}
			}
			if (Math.Abs(xyz2.Y) < 0.0001)
			{
				if (xyz2.X == 0.0)
				{
					num = 0.0;
				}
				if (xyz2.X > 0.0)
				{
					num = 0.0;
				}
				if (xyz2.X < 0.0)
				{
					num = 180.0;
				}
			}
			if ((xyz2.X > 0.0 && xyz2.Y > 0.0) || (xyz2.X < 0.0 && xyz2.Y > 0.0))
			{
				num = Convert.ToDouble(StringTools.CutOffEfficacious((new XYZ(1.0, 0.0, 0.0).AngleTo(xyz2) * 180.0 / Math.PI).ToString(), 2));
			}
			if ((xyz2.X > 0.0 && xyz2.Y < 0.0) || (xyz2.X < 0.0 && xyz2.Y < 0.0))
			{
				num = new XYZ(1.0, 0.0, 0.0).AngleTo(xyz2) * 180.0 / Math.PI;
				num = Convert.ToDouble(StringTools.CutOffEfficacious((360.0 - num).ToString(), 2));
			}
			if (num == 360.0)
			{
				num = 0.0;
			}
			num = Convert.ToDouble(StringTools.CutOffEfficacious(num.ToString(), 0));
			this.textBoxAngel.Text = num.ToString();
		}

		public FamilySymbol fs;

		public double OffSet;

		private Document doc;

		private HVACDataReader hvacDR;

		private TreeNode treeSelectedNode;

		public PlumbingFixturesParam plumbingFixturesPara = new PlumbingFixturesParam();

		public LayLineParameter LineParameter = new LayLineParameter();

		private TreeView treeView = new TreeView();

		public int layTerminalType;
	}
}
